// depend on validation
//$.validator.addMethod("dependon", function (value, element, param) {
//    return ture;
//});
$.validator.unobtrusive.adapters.add("dependon", ["dependon", "action", "controller", "area", "additionalfields"],
    function (options) {
        var fields = options.params.dependon.split(',');
        var url = "/" + options.params.controller + "/" + options.params.action;
        if (options.params.area)
            url = "/" + options.params.area + url;

        var eles = options.params.additionalfields.split(',');
        var target = $('#' + options.element.name);

        if (target && typeof (target.dependonchanged) != 'function') {
            if (target.is('select')) {
                target.bind('dependonchanged', function (evt, data) {
                    $(this).empty();
                    $(this).append('<option value=""></option>');
                    if (data) {
                        for (var i = 0; i < data.length; i++) {
                            var option = '<option value="' + data.Value + '"';
                            if (data.Selected) {
                                option += ' selected="selected"';
                            }
                            option += '">' + data.Text + '</option>';
                            $(this).append(option);
                        }
                    }
                });
            }
        }

        $.each(fields, function (i, field) {
            field = $.trim(field);
            $('#' + field).blur(function () {
                var data = {};
                eval("data." + field + "='" + $(this).val() + "'");
                if (eles && eles.length > 0) {
                    $.each(eles, function (i, ele) {
                        ele = $.trim(ele);
                        eval("data." + ele + "='" + $('#' + ele).val() + "'");
                    });
                }
                $.ajax({ url: url, dataType: 'json', data: data,
                    success: function (data) {
                        target.trigger('dependonchanged', [data]);
                    }
                });
            });
        });
    }
);

// readonly on create or edit
    $.validator.unobtrusive.adapters.add("readonlyon", ["oncreate", "onedit", "additionalfields"],
function (options) {
    var eles = options.params.additionalfields.split(',');
    var oncreate = options.params.oncreate.toLowerCase() == "true" || false;
    var onedit = options.params.onedit.toLowerCase() == "true" || false;
    var target = $('#' + options.element.name);
    var isOnCreat = true;
    for (var i = 0; i < eles.length; i++) {
        var val = $('#' + eles[i]).val();
        if (val && $.trim(val) != '') {
            isOnCreat = false;
            break;
        }
    }

    if (isOnCreat && oncreate) {
        target.attr('disabled', 'disabled');
    }
    if (!isOnCreat && onedit) {
        target.attr('disabled', 'disabled');
    }
});


